package com.ruoyi.web.controller.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.token.sqlToken;
import com.ruoyi.web.core.config.sqlYz.LoginRequired;
import com.ruoyi.web.domian.sqlReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/cpt_sql")
public class sqlCptController extends BaseController {

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IdUserService IdUserService;
    @Autowired
    private ICompanyTypeService ICompanyTypeService;
    @Autowired
    private IProductTypeService IProductTypeService;

    public boolean headerPost(HttpServletRequest req){

        if(req.getHeader("token") != null){
            dUser dUser = new dUser();
            dUser.setUserToken(req.getHeader("token"));
            System.out.println(dUser.getToken());
            System.out.println("\t");
            List<dUser> a= IdUserService.selectdUserList(dUser);
            System.out.println(a);
            if(a.size()>0){
                return true;
            }
        }
        return false;
    }


//    @LoginRequired
//公司信息查找
    @PostMapping("/getCpt")
    @ResponseBody

    public sqlReturn getCpt(HttpServletRequest req,Company company){
        boolean sqlReturn= headerPost (req);
        if(!sqlReturn){
            return new sqlReturn(null,"登陆过期",400);
        }
        try {
            List<Company> list = companyService.selectCompanyList(company);
            if(list.size() == 0){
                return new sqlReturn(null,"暂无公司信息",400);
            }
            return new sqlReturn(list,"查询成功",200);
        }catch (AssertionError e){

            return new sqlReturn(null,"网络异常",500);
        }
    }

//    查找公司 类型
    @GetMapping("/cpt_type")
    @ResponseBody
    public sqlReturn getCptType(){
        List<CompanyType> list = ICompanyTypeService.selectCompanyTypeList(null);
        System.out.println(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("cpt_type",list);
        return new sqlReturn(map,"成功",200);
    }

//    查找产品类型
@GetMapping("/pct_type")
@ResponseBody
public sqlReturn getpct_type(){
    ProductType ProductType=new ProductType();
    List<ProductType> list = IProductTypeService.selectProductTypeList(ProductType);
    System.out.println(list);
    HashMap<String, Object> map = new HashMap<>();
    map.put("pct_type",list);
    return new sqlReturn(map,"成功",200);
}


    //更据公司类型 查找推荐公司
    @PostMapping("/cpt_tuijian")
    @ResponseBody
    public sqlReturn getCptTuijian(Company company,String cpId_not){
        List<Company> list = companyService.selectCompanyList(company);
//        遍历 list 移除 cpId_not 相匹配的对象
        if(list.size() == 1 && list.get(0).getCpId().toString()==cpId_not){
            return new sqlReturn(null,"暂无推荐公司",400);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        return new sqlReturn(map,"推荐公司",200);
    }
}



